// 时间戳 2024-02-03 05:15
export const convertToTimestamp = (dateString)=>{
    // 将字符串转换为 Date 对象
    const date = new Date(dateString.replace(' ', 'T')); // 替换空格为 T 以符合 ISO 8601 格式
    
    // 检查是否成功创建 Date 对象
    if (isNaN(date.getTime())) {
        throw new Error("Invalid date format");
    }

    // 返回时间戳（以毫秒为单位）
    return date.getTime();
}

// 当前时间剩余的时间戳
export const getRemainingTimeToTimestamp = (targetDateString)=>{
    // 将指定时间字符串转换为 Date 对象
    const targetDate = new Date(targetDateString.replace(' ', 'T'));
    
    // 获取当前时间的 Date 对象
    const currentDate = new Date();
    
    // 计算剩余时间（以毫秒为单位）
    const remainingTime = targetDate.getTime() - currentDate.getTime();
    
    // 检查目标时间是否已过去
    if (remainingTime < 0) {
        //throw new Error("The target date has already passed");
		return 0;
    }
    
    return remainingTime;
}

export const formatTimestamp = (timestamp,type)=>{
	timestamp = Number(timestamp)
    const date = new Date(timestamp * 1000); // 时间戳单位是秒，所以乘以 1000
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
	console.log('year', year)
	//年月日
	if(type === 1) {
		return `${year}-${month}-${day}`;
	}
	//月
	if(type === 2) {
		return `${year}.${month}`;
	}
	//天
	if(type === 3) {
		return `${day}`;
	}
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}